package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.dictionary.dao.db.Country;
import com.gzhryc.dictionary.services.CountryService;
import com.gzhryc.dictionary.services.dto.CountrySearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class CountryController {

	@Route("/config/country/list.html")
	public String list(HttpServletRequest request,HttpServletResponse response){
		return "/config/country/list.html";
	}
	
	@Route("/config/country/list.data")
	public ListResult<Country> listData(HttpServletRequest request){
		ListResult<Country> result = new ListResult<Country>(DtoConstants.SUCCESS);
		CountrySearch search = WebUtils.getParameter(request,CountrySearch.class);
		Integer pageNo = WebUtils.getInteger(request, "page",1);
		Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
		result.getBody().setTotal(CountryService.self().countBySearch(search));
		List<Country> data = CountryService.self().findBySearch(search, pageNo, pageLimit);
		if(data != null){
			result.getBody().setData(data);
		}
		return result;
	}
	
	@Route("/config/country/add.html")
	public String add(HttpServletRequest request,HttpServletResponse response){
		return "/config/country/edit.html";
	}
	
	@Route(value="/config/country/add.do",method="post")
	public AjaxResult doAdd(HttpServletRequest request,HttpServletResponse response){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败");
		Country country = WebUtils.getParameter(request, Country.class);
		try {
			if(CountryService.self().add(country)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Route("/config/country/update.html")
	public String update(HttpServletRequest request,HttpServletResponse response){
		Long id = WebUtils.getLong(request, "id");
		if(id != null) {
			Country country = CountryService.self().getById(id);
			WebUtils.setAttribute(request, country);
		}
		return "/config/country/edit.html";
	}
	
	@Route(value="/config/country/update.do",method="post")
	public AjaxResult doUpdate(HttpServletRequest request,HttpServletResponse response){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败");
		Country country = WebUtils.getParameter(request, Country.class);
		try {
			if(CountryService.self().updateNotNull(country)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Route("/config/country/del.do")
	public AjaxResult delete(HttpServletRequest request,HttpServletResponse response){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败");
		Long id = WebUtils.getLong(request, "id");
		try {
			if(CountryService.self().deleteById(id)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
}
